<?php
namespace App\Http\Controllers\Api\Repositories;

use App\Models\User\WxUserFollow as Model;
use App\Models\User\WxUser;
use App\Wen\Utils\FieldUtils;
use App\Wen\Utils\UserUtils;
use Illuminate\Support\Facades\DB;

class WxUserFollowRepositores
{
    /**
     * Model.
     *
     * @var string
     */
    protected $eloquentClass = Model::class;

    /**
     * 点赞
     */
    public static function follow($uid, $user_follow_id)
    {
        DB::beginTransaction();
        try {

            $isExists = Model::where('user_follow_id', $user_follow_id)
                ->where('user_id', $uid)
                ->exists();
            if (!$isExists) {

                // 不能关注自己
                if ($user_follow_id != $uid) {
                    // 关注
                    $wxLike = new Model();
                    $wxLike->user_follow_id = $user_follow_id;
                    $wxLike->user_id = $uid;
                    $wxLike->save();
                    $user = WxUser::where('id', $uid)->first(FieldUtils::userInfoColums());
                    UserUtils::add_user_notice(4004, $user_follow_id, '新增了一个粉丝', '「' . '<a href="/pages/user/user?id='.$user['id'].'">'.$user['user_name'].'</a>' . '」悄悄的关注了您。', 101, -$uid);
                }

            }
            DB::commit();

            return true;
        } catch (\Exception $e) {
            DB::rollBack();
            _logger_(__file__, __line__, $e->getMessage());
            return false;
        }

    }

    public static function unfollow($uid, $user_follow_id)
    {
        DB::beginTransaction();
        try {

            (new Model())->where('user_follow_id', $user_follow_id)
                ->where('user_id', $uid)
                ->delete();
            DB::commit();
            return true;

        } catch (\Exception $e) {
            DB::rollBack();
            _logger_(__file__, __line__, $e->getMessage());
            return false;
        }
    }



}
